package com.demo.security.model;

import java.util.Collection;
import java.util.List;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.demo.security.entity.SysUser;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
public class SysUserDetails implements UserDetails {

    private static final long serialVersionUID = 1L;

    private SysUser user;
    private List<GrantedAuthority> authorities;

    public SysUserDetails() {
        super();
    }

    public SysUserDetails(SysUser user) {
        super();
        this.user = user;
    }

    public SysUserDetails setAuthorities(List<GrantedAuthority> authorities) {
        this.authorities = authorities;
        return this;
    }

    public SysUser getUser() {
        return user;
    }

    public void setUser(SysUser user) {
        this.user = user;
    }

    // 获取用户权限列表
    @Override
    public Collection<GrantedAuthority> getAuthorities() {
        return this.authorities;
    }

    // 获取校验用户名
    @Override
    public String getUsername() {
        return user.getUsername();
    }

    // 获取校验用密码
    @Override
    @JsonIgnore
    public String getPassword() {
        return user.getPassword();
    }

    // 账户是否未过期
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    // 账户是否未锁定
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    // 帐户密码是否未过期，一般有的密码要求性高的系统会使用到，比较每隔一段时间就要求用户重置密码
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    // 账户是否可用
    @Override
    public boolean isEnabled() {
        return user.isEnabled();
    }
}